<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form v-bind="formLayout">
        <!-- <a-row :gutter="48">
          <a-col :md="10" :sm="24">
            <a-form-item
              label="状态" >
              <a-radio-group v-model="queryParam.replyFlag" button-style="solid" @change="$refs.table.refresh(true)">
                <a-radio-button :value="-1">
                  全部
                </a-radio-button>
                <a-radio-button :value="0">
                  未回复
                </a-radio-button>
                <a-radio-button :value="1">
                  已回复
                </a-radio-button>
              </a-radio-group>
            </a-form-item>
          </a-col>
        </a-row> -->
        <a-row :gutter="48">
          <a-col :md="10" :sm="24">
            <a-form-item
              label="关键字">
              <a-input v-model="queryParam.keyword" placeholder="请输入关键词" @keyup.enter.native="$refs.table.refresh(true)"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="setEmpty">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div v-if="!$store.getters.isMzRole" class="table-operator">
      <span style="margin-right: 16px;">已选中{{ selectedRowKeys.length }}行</span>
      <a-button icon="delete" @click="handleDeleteBatch">删除</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :rowSelection="rowSelection"
      showPagination="auto"
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
      <span slot="message" slot-scope="text">
        <ellipsis :length="50" tooltip>{{ text }}</ellipsis>
      </span>
      <!-- <span slot="reply" slot-scope="text">
        <ellipsis :length="50" tooltip>{{ text }}</ellipsis>
      </span> -->

      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleCheck(record)">查看</a>
          <!-- <a-divider type="vertical" />
          <a @click="handleReply(record)">回复</a> -->
          <a-divider type="vertical" />
          <a @click="handleDelete(record)">删除</a>
        </template>
      </span>
    </s-table>

    <create-form
      ref="createModal"
      :visible="visible"
      :loading="confirmLoading"
      :model="mdl"
      @cancel="handleCancel"
      @ok="handleOk"
    />
    <check-form
      ref="checkModal"
    />
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getMsg, saveMsg, deleteMsg } from '@/api/msg'

import CreateForm from './CreateForm'
import CheckForm from './CheckForm'

let columns = [
  {
    title: '序号',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '留言',
    dataIndex: 'message',
    scopedSlots: { customRender: 'message' }
  },
  {
    title: '留言人',
    dataIndex: 'createUserName'
  },
  {
    title: '留言时间',
    dataIndex: 'createTime'
  },
  // {
  //   title: '回复',
  //   dataIndex: 'reply',
  //   scopedSlots: { customRender: 'reply' }
  // },
  // {
  //   title: '回复时间',
  //   dataIndex: 'replyTime'
  // },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'NoticeMsg',
  components: {
    STable,
    Ellipsis,
    CreateForm,
    CheckForm
  },
  data () {
  this.formLayout = {
      labelAlign: 'left',
      labelCol: {
        span: 4
      },
      wrapperCol: {
        span: 20
      }
    }
    // 民政角色不展示操作项
    if (this.$store.getters.isMzRole) {
      columns = columns.filter(v => v.dataIndex != 'action')
    }
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 查询参数
      queryParam: { replyFlag: -1 },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        console.log('loadData request parameters:', requestParameters)
        if (requestParameters.replyFlag === -1) {
          delete requestParameters.replyFlag
        }
        return getMsg(requestParameters)
          .then(res => {
            console.log(res)
            return res
          })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    setEmpty () {
      this.queryParam = { replyFlag: -1 }
      this.$refs.table.refresh(true)
    },
    handleDeleteBatch () {
      if (this.selectedRowKeys.length === 0) {
        this.$warning({
          title: '提示',
          content: '请选择需要删除的数据'
        })
      } else {
        console.log(this.selectedRowKeys)
        deleteMsg({
            id: this.selectedRowKeys.join(',')
          }).then(res => {
              // 刷新表格
              this.$refs.table.refresh()
              this.$message.success('批量删除成功')
            })
      }
    },
    handleReply (record) {
      this.mdl = { ...record }
      this.visible = true
    },
    handleCheck (record) {
      this.$refs.checkModal.checkView(record)
    },
    handleDelete (record) {
      this.$confirm({
        title: '删除确认',
        content: '确认要删除该数据吗？并且不可恢复！',
        okType: 'danger',
        onOk: () => {
          deleteMsg({
            id: record.id
          }).then(res => {
              // 刷新表格
              this.$refs.table.refresh()
              this.$message.success('删除成功')
            })
        }
      })
    },
    handleOk () {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('values', values)
          saveMsg(values).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success(values.id > 0 ? '回复成功' : '新增成功')
            }).finally(() => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false

      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>

<style lang="less" scoped>
:deep(.ant-radio-button-wrapper ) {
  margin-right: 8px;
    &:first-child {
      border-radius: 0;
    }
    &:last-child {
      border-radius: 0;
    }
  }
</style>
